package nku.csc364;

/* Raj Akula
 * 
 * AddRecordDialog.java
 *
 * A dialog to allow the user to input the 6 fields for the record.
 * Includes validations per requirements of the assignment.
 * **First time using a gui in this way. 
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class AddRecordDialog extends JDialog implements ActionListener{
	
	//Instance data for this class, for panel, for returning person and for text fields
	private JPanel myPanel = null;
	Person person ;
	JTextField lnameTextField;
    JTextField fnameTextField ;
    JTextField ssnTextField ;
    JTextField phoneTextField ;
    JTextField genderTextField ;
    JTextField dobTextField ;
    
    
	// Constructor to build frame
	public AddRecordDialog(JFrame frame) {
		//super(frame, modal);
		myPanel = new JPanel();
		getContentPane().add(myPanel);
		addComponentsToPane(getContentPane());
		setTitle("Add a record");
		setModal(true);
		setResizable(false);
		pack();
		setLocationRelativeTo(frame);
		setVisible(true);
	}
	
	// get person 
	public Person getPerson() {
		return person;
	}

	
	// Method to create a layout
	public void addComponentsToPane(Container pane) { 
		pane.setLayout(null);
        pane.setPreferredSize(new Dimension(300,400));
        
        //display label, butons and textboxes
        JLabel lnameLabel = new JLabel("Last Name: ");
        lnameTextField = new JTextField(10);
        JLabel fnameLabel = new JLabel("First Name: ");
        fnameTextField = new JTextField(10);
        JLabel ssnLabel = new JLabel("SSN: ");
        ssnTextField = new JTextField(10);
        JLabel phoneLabel = new JLabel("Phone Number: ");
        phoneTextField = new JTextField(0);
        JLabel genderLabel = new JLabel("Gender: ");
        genderTextField = new JTextField(3);
        JLabel dobLabel = new JLabel("Birth Date: ");
        dobTextField = new JTextField(10);
        JButton addButton = new JButton("Add");
        JButton cancelButton = new JButton("Cancel");
        
        
        
        // Add labels to pane
        pane.add(lnameLabel);
        pane.add(fnameLabel);
        pane.add(ssnLabel);
        pane.add(phoneLabel);
        pane.add(genderLabel);
        pane.add(dobLabel);
        
        //Add textFields to pane
        pane.add(lnameTextField);
        pane.add(fnameTextField);
        pane.add(ssnTextField);
        pane.add(phoneTextField);
        pane.add(genderTextField);
        pane.add(dobTextField);
        pane.add(addButton);
        pane.add(cancelButton);
        
        //get fields 
        Insets insets = pane.getInsets();
       
       	// FOR SIZE
        Dimension labelSize=lnameLabel.getPreferredSize();
        Dimension phoneLabelSize=phoneLabel.getPreferredSize();
        Dimension textFieldSize = lnameTextField.getPreferredSize();
        Dimension fnameLabelSize = fnameLabel.getPreferredSize();
        Dimension addButtonSize = addButton.getPreferredSize();
        Dimension cancelButtonSize = cancelButton.getPreferredSize();
        
        
        //Layout
        lnameLabel.setBounds(25 + insets.left, 5+insets.top,labelSize.width,labelSize.height);
        lnameTextField.setBounds(120 + insets.left, 5+insets.top,textFieldSize.width,textFieldSize.height);
        
        fnameLabel.setBounds(25 + insets.left, 25+insets.top,fnameLabelSize.width,fnameLabelSize.height);
        fnameTextField.setBounds(120 + insets.left, 25+insets.top,textFieldSize.width,textFieldSize.height);
        
        ssnLabel.setBounds(25 + insets.left, 45+insets.top,labelSize.width,labelSize.height);
        ssnTextField.setBounds(120 + insets.left, 45+insets.top,textFieldSize.width,textFieldSize.height);
        
        phoneLabel.setBounds(25 + insets.left, 65+insets.top,phoneLabelSize.width,phoneLabelSize.height);
        phoneTextField.setBounds(120 + insets.left, 65+insets.top,textFieldSize.width,textFieldSize.height);
        
        genderLabel.setBounds(25 + insets.left, 85+insets.top,labelSize.width,labelSize.height);
        genderTextField.setBounds(120 + insets.left, 85+insets.top,genderTextField.getPreferredSize().width,genderTextField.getPreferredSize().height);
        
        dobLabel.setBounds(25 + insets.left, 105+insets.top,labelSize.width,labelSize.height);
        dobTextField.setBounds(120 + insets.left, 105+insets.top,textFieldSize.width,textFieldSize.height);
        
        
        addButton.setBounds(25 + insets.left, 145+insets.top,addButtonSize.width,addButtonSize.height);
        addButton.addActionListener(this);
        
        cancelButton.setBounds(120 + insets.left, 145+insets.top,cancelButtonSize.width,cancelButtonSize.height);
        cancelButton.addActionListener(this);
        
        
        
        
    }
    
     // Action listners
	 public void actionPerformed(ActionEvent e) {
		 if (e.getActionCommand().equals("Add")){
			 doAddAction();
		 }
		 if (e.getActionCommand().equals("Cancel")){
			 this.dispose();
		 }
		 
	    }
	 // Doing this on add
	 private void doAddAction(){
		 // get all fields for person
		 char genderText = ' ';
		 String genderInput = genderTextField.getText();
		 if (genderInput.equals("") || genderInput.length() > 1){
			 genderText=' ';
		 }else{
			 genderText=genderInput.charAt(0);
		 }
		 person = new Person(fnameTextField.getText(),
					lnameTextField.getText(),
					ssnTextField.getText(),
					phoneTextField.getText(),
					genderText,
					dobTextField.getText()
					)	;
		
	    
	     //validate user input
	     if (validateInput(person)){
	    	
	    	 this.setVisible(false);
	    	 
	     }else{
	    	 person=null;
	     }
	 }
	 
	 // Method to validate each one of the data fields. 
	 private boolean validateInput(Person person){
		 boolean result = true;
		 

		 if (!InputValidator.isLnameValidated(person.getLastName())){
			 JOptionPane.showMessageDialog(this, "Invalid Last name", "Error", JOptionPane.ERROR_MESSAGE);
			 result=false;
		 }
		 
		 if (!InputValidator.isFnameValidated(person.getFirstName())){
			 JOptionPane.showMessageDialog(this, "Invalid First name", "Error", JOptionPane.ERROR_MESSAGE);
			 result=false;
		 }
		 
		 if (!InputValidator.isSsnValidated(person.getSsn())){
			 JOptionPane.showMessageDialog(this, "Invalid SSN, must be 9 digit number", "Error", JOptionPane.ERROR_MESSAGE);
			 result=false;
		 }
		 if (!InputValidator.isPhoneValidated(String.valueOf(person.getPhoneNo()))){
			 JOptionPane.showMessageDialog(this, "Invalid Phone number, must be 10 digits", "Error", JOptionPane.ERROR_MESSAGE);
			 result=false;
		 }
		 if (!InputValidator.isGenderValidated(String.valueOf(person.getGender()))){
			 JOptionPane.showMessageDialog(this, "Invalid Gender (use M or F)", "Error", JOptionPane.ERROR_MESSAGE);
			 result=false;
		 }
		 if (!InputValidator.isBirthDateValidated(String.valueOf(person.getDob()))){
			 JOptionPane.showMessageDialog(this, "Invalid birth day , must be older than 16 and have the format: (m/d/yy)", "Error", JOptionPane.ERROR_MESSAGE);
			 result=false;
		 }
		 
		 return result;
	 }
	 
}
